<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/syncedmem.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../index.html">top level</a> - <a href="index.html">src/caffe</a> - syncedmem.cpp<span style="font-size: 80%;"> (source / <a href="syncedmem.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">36</td>
            <td class="headerCovTableEntry">50</td>
            <td class="headerCovTableEntryLo">72.0 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:25:26</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">8</td>
            <td class="headerCovTableEntry">12</td>
            <td class="headerCovTableEntryLo">66.7 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &quot;caffe/common.hpp&quot;</a>
<span class="lineNum">       2 </span>            : #include &quot;caffe/syncedmem.hpp&quot;
<span class="lineNum">       3 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<a name="4"><span class="lineNum">       4 </span>            : </a>
<span class="lineNum">       5 </span>            : namespace caffe {
<span class="lineNum">       6 </span><span class="lineNoCov">          0 : SyncedMemory::SyncedMemory()</span>
<span class="lineNum">       7 </span>            :   : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(0), head_(UNINITIALIZED),
<span class="lineNum">       8 </span><span class="lineNoCov">          0 :     own_cpu_data_(false), cpu_malloc_use_cuda_(false), own_gpu_data_(false) {</span>
<span class="lineNum">       9 </span>            : #ifndef CPU_ONLY
<span class="lineNum">      10 </span>            : #ifdef DEBUG
<span class="lineNum">      11 </span>            :   CUDA_CHECK(cudaGetDevice(&amp;device_));
<span class="lineNum">      12 </span>            : #endif
<span class="lineNum">      13 </span>            : #endif
<a name="14"><span class="lineNum">      14 </span><span class="lineNoCov">          0 : }</span></a>
<span class="lineNum">      15 </span>            : 
<span class="lineNum">      16 </span><span class="lineCov">        156 : SyncedMemory::SyncedMemory(size_t size)</span>
<span class="lineNum">      17 </span>            :   : cpu_ptr_(NULL), gpu_ptr_(NULL), size_(size), head_(UNINITIALIZED),
<span class="lineNum">      18 </span><span class="lineCov">        156 :     own_cpu_data_(false), cpu_malloc_use_cuda_(false), own_gpu_data_(false) {</span>
<span class="lineNum">      19 </span>            : #ifndef CPU_ONLY
<span class="lineNum">      20 </span>            : #ifdef DEBUG
<span class="lineNum">      21 </span>            :   CUDA_CHECK(cudaGetDevice(&amp;device_));
<span class="lineNum">      22 </span>            : #endif
<span class="lineNum">      23 </span>            : #endif
<a name="24"><span class="lineNum">      24 </span><span class="lineCov">        156 : }</span></a>
<span class="lineNum">      25 </span>            : 
<span class="lineNum">      26 </span><span class="lineCov">        312 : SyncedMemory::~SyncedMemory() {</span>
<span class="lineNum">      27 </span><span class="lineCov">        156 :   check_device();</span>
<span class="lineNum">      28 </span><span class="lineCov">        156 :   if (cpu_ptr_ &amp;&amp; own_cpu_data_) {</span>
<span class="lineNum">      29 </span>            :     CaffeFreeHost(cpu_ptr_, cpu_malloc_use_cuda_);
<span class="lineNum">      30 </span>            :   }
<span class="lineNum">      31 </span>            : 
<span class="lineNum">      32 </span>            : #ifndef CPU_ONLY
<span class="lineNum">      33 </span>            :   if (gpu_ptr_ &amp;&amp; own_gpu_data_) {
<span class="lineNum">      34 </span>            :     CUDA_CHECK(cudaFree(gpu_ptr_));
<span class="lineNum">      35 </span>            :   }
<span class="lineNum">      36 </span>            : #endif  // CPU_ONLY
<a name="37"><span class="lineNum">      37 </span><span class="lineCov">        156 : }</span></a>
<span class="lineNum">      38 </span>            : 
<span class="lineNum">      39 </span><span class="lineCov">     339921 : inline void SyncedMemory::to_cpu() {</span>
<span class="lineNum">      40 </span><span class="lineCov">     339921 :   check_device();</span>
<span class="lineNum">      41 </span><span class="lineCov">     339922 :   switch (head_) {</span>
<span class="lineNum">      42 </span>            :   case UNINITIALIZED:
<span class="lineNum">      43 </span><span class="lineCov">         99 :     CaffeMallocHost(&amp;cpu_ptr_, size_, &amp;cpu_malloc_use_cuda_);</span>
<span class="lineNum">      44 </span><span class="lineCov">         99 :     caffe_memset(size_, 0, cpu_ptr_);</span>
<span class="lineNum">      45 </span><span class="lineCov">         99 :     head_ = HEAD_AT_CPU;</span>
<span class="lineNum">      46 </span><span class="lineCov">         99 :     own_cpu_data_ = true;</span>
<span class="lineNum">      47 </span><span class="lineCov">         99 :     break;</span>
<span class="lineNum">      48 </span>            :   case HEAD_AT_GPU:
<span class="lineNum">      49 </span>            : #ifndef CPU_ONLY
<span class="lineNum">      50 </span>            :     if (cpu_ptr_ == NULL) {
<span class="lineNum">      51 </span>            :       CaffeMallocHost(&amp;cpu_ptr_, size_, &amp;cpu_malloc_use_cuda_);
<span class="lineNum">      52 </span>            :       own_cpu_data_ = true;
<span class="lineNum">      53 </span>            :     }
<span class="lineNum">      54 </span>            :     caffe_gpu_memcpy(size_, gpu_ptr_, cpu_ptr_);
<span class="lineNum">      55 </span>            :     head_ = SYNCED;
<span class="lineNum">      56 </span>            : #else
<span class="lineNum">      57 </span><span class="lineNoCov">          0 :     NO_GPU;</span>
<span class="lineNum">      58 </span>            : #endif
<span class="lineNum">      59 </span>            :     break;
<span class="lineNum">      60 </span>            :   case HEAD_AT_CPU:
<span class="lineNum">      61 </span>            :   case SYNCED:
<span class="lineNum">      62 </span>            :     break;
<span class="lineNum">      63 </span>            :   }
<span class="lineNum">      64 </span><span class="lineCov">     339922 : }</span>
<span class="lineNum">      65 </span>            : 
<span class="lineNum">      66 </span>            : inline void SyncedMemory::to_gpu() {
<span class="lineNum">      67 </span>            :   check_device();
<span class="lineNum">      68 </span>            : #ifndef CPU_ONLY
<span class="lineNum">      69 </span>            :   switch (head_) {
<span class="lineNum">      70 </span>            :   case UNINITIALIZED:
<span class="lineNum">      71 </span>            :     CUDA_CHECK(cudaMalloc(&amp;gpu_ptr_, size_));
<span class="lineNum">      72 </span>            :     caffe_gpu_memset(size_, 0, gpu_ptr_);
<span class="lineNum">      73 </span>            :     head_ = HEAD_AT_GPU;
<span class="lineNum">      74 </span>            :     own_gpu_data_ = true;
<span class="lineNum">      75 </span>            :     break;
<span class="lineNum">      76 </span>            :   case HEAD_AT_CPU:
<span class="lineNum">      77 </span>            :     if (gpu_ptr_ == NULL) {
<span class="lineNum">      78 </span>            :       CUDA_CHECK(cudaMalloc(&amp;gpu_ptr_, size_));
<span class="lineNum">      79 </span>            :       own_gpu_data_ = true;
<span class="lineNum">      80 </span>            :     }
<span class="lineNum">      81 </span>            :     caffe_gpu_memcpy(size_, cpu_ptr_, gpu_ptr_);
<span class="lineNum">      82 </span>            :     head_ = SYNCED;
<span class="lineNum">      83 </span>            :     break;
<span class="lineNum">      84 </span>            :   case HEAD_AT_GPU:
<span class="lineNum">      85 </span>            :   case SYNCED:
<span class="lineNum">      86 </span>            :     break;
<span class="lineNum">      87 </span>            :   }
<span class="lineNum">      88 </span>            : #else
<span class="lineNum">      89 </span>            :   NO_GPU;
<span class="lineNum">      90 </span>            : #endif
<a name="91"><span class="lineNum">      91 </span>            : }</a>
<span class="lineNum">      92 </span>            : 
<span class="lineNum">      93 </span><span class="lineCov">     283408 : const void* SyncedMemory::cpu_data() {</span>
<span class="lineNum">      94 </span><span class="lineCov">     283408 :   check_device();</span>
<span class="lineNum">      95 </span><span class="lineCov">     283408 :   to_cpu();</span>
<span class="lineNum">      96 </span><span class="lineCov">     283408 :   return (const void*)cpu_ptr_;</span>
<a name="97"><span class="lineNum">      97 </span>            : }</a>
<span class="lineNum">      98 </span>            : 
<span class="lineNum">      99 </span><span class="lineCov">      10500 : void SyncedMemory::set_cpu_data(void* data) {</span>
<span class="lineNum">     100 </span><span class="lineCov">      10500 :   check_device();</span>
<span class="lineNum">     101 </span><span class="lineCov">      21000 :   CHECK(data);</span>
<span class="lineNum">     102 </span><span class="lineCov">      10500 :   if (own_cpu_data_) {</span>
<span class="lineNum">     103 </span><span class="lineNoCov">          0 :     CaffeFreeHost(cpu_ptr_, cpu_malloc_use_cuda_);</span>
<span class="lineNum">     104 </span>            :   }
<span class="lineNum">     105 </span><span class="lineCov">      10500 :   cpu_ptr_ = data;</span>
<span class="lineNum">     106 </span><span class="lineCov">      10500 :   head_ = HEAD_AT_CPU;</span>
<span class="lineNum">     107 </span><span class="lineCov">      10500 :   own_cpu_data_ = false;</span>
<a name="108"><span class="lineNum">     108 </span><span class="lineCov">      10500 : }</span></a>
<span class="lineNum">     109 </span>            : 
<span class="lineNum">     110 </span><span class="lineNoCov">          0 : const void* SyncedMemory::gpu_data() {</span>
<span class="lineNum">     111 </span><span class="lineNoCov">          0 :   check_device();</span>
<span class="lineNum">     112 </span>            : #ifndef CPU_ONLY
<span class="lineNum">     113 </span>            :   to_gpu();
<span class="lineNum">     114 </span>            :   return (const void*)gpu_ptr_;
<span class="lineNum">     115 </span>            : #else
<span class="lineNum">     116 </span><span class="lineNoCov">          0 :   NO_GPU;</span>
<span class="lineNum">     117 </span>            :   return NULL;
<span class="lineNum">     118 </span>            : #endif
<a name="119"><span class="lineNum">     119 </span>            : }</a>
<span class="lineNum">     120 </span>            : 
<span class="lineNum">     121 </span><span class="lineNoCov">          0 : void SyncedMemory::set_gpu_data(void* data) {</span>
<span class="lineNum">     122 </span><span class="lineNoCov">          0 :   check_device();</span>
<span class="lineNum">     123 </span>            : #ifndef CPU_ONLY
<span class="lineNum">     124 </span>            :   CHECK(data);
<span class="lineNum">     125 </span>            :   if (own_gpu_data_) {
<span class="lineNum">     126 </span>            :     CUDA_CHECK(cudaFree(gpu_ptr_));
<span class="lineNum">     127 </span>            :   }
<span class="lineNum">     128 </span>            :   gpu_ptr_ = data;
<span class="lineNum">     129 </span>            :   head_ = HEAD_AT_GPU;
<span class="lineNum">     130 </span>            :   own_gpu_data_ = false;
<span class="lineNum">     131 </span>            : #else
<span class="lineNum">     132 </span><span class="lineNoCov">          0 :   NO_GPU;</span>
<span class="lineNum">     133 </span>            : #endif
<a name="134"><span class="lineNum">     134 </span>            : }</a>
<span class="lineNum">     135 </span>            : 
<span class="lineNum">     136 </span><span class="lineCov">      56593 : void* SyncedMemory::mutable_cpu_data() {</span>
<span class="lineNum">     137 </span><span class="lineCov">      56593 :   check_device();</span>
<span class="lineNum">     138 </span><span class="lineCov">      56591 :   to_cpu();</span>
<span class="lineNum">     139 </span><span class="lineCov">      56591 :   head_ = HEAD_AT_CPU;</span>
<span class="lineNum">     140 </span><span class="lineCov">      56591 :   return cpu_ptr_;</span>
<a name="141"><span class="lineNum">     141 </span>            : }</a>
<span class="lineNum">     142 </span>            : 
<span class="lineNum">     143 </span><span class="lineNoCov">          0 : void* SyncedMemory::mutable_gpu_data() {</span>
<span class="lineNum">     144 </span><span class="lineNoCov">          0 :   check_device();</span>
<span class="lineNum">     145 </span>            : #ifndef CPU_ONLY
<span class="lineNum">     146 </span>            :   to_gpu();
<span class="lineNum">     147 </span>            :   head_ = HEAD_AT_GPU;
<span class="lineNum">     148 </span>            :   return gpu_ptr_;
<span class="lineNum">     149 </span>            : #else
<span class="lineNum">     150 </span><span class="lineNoCov">          0 :   NO_GPU;</span>
<span class="lineNum">     151 </span>            :   return NULL;
<span class="lineNum">     152 </span>            : #endif
<span class="lineNum">     153 </span>            : }
<span class="lineNum">     154 </span>            : 
<span class="lineNum">     155 </span>            : #ifndef CPU_ONLY
<span class="lineNum">     156 </span>            : void SyncedMemory::async_gpu_push(const cudaStream_t&amp; stream) {
<span class="lineNum">     157 </span>            :   check_device();
<span class="lineNum">     158 </span>            :   CHECK(head_ == HEAD_AT_CPU);
<span class="lineNum">     159 </span>            :   if (gpu_ptr_ == NULL) {
<span class="lineNum">     160 </span>            :     CUDA_CHECK(cudaMalloc(&amp;gpu_ptr_, size_));
<span class="lineNum">     161 </span>            :     own_gpu_data_ = true;
<span class="lineNum">     162 </span>            :   }
<span class="lineNum">     163 </span>            :   const cudaMemcpyKind put = cudaMemcpyHostToDevice;
<span class="lineNum">     164 </span>            :   CUDA_CHECK(cudaMemcpyAsync(gpu_ptr_, cpu_ptr_, size_, put, stream));
<span class="lineNum">     165 </span>            :   // Assume caller will synchronize on the stream before use
<span class="lineNum">     166 </span>            :   head_ = SYNCED;
<span class="lineNum">     167 </span>            : }
<a name="168"><span class="lineNum">     168 </span>            : #endif</a>
<span class="lineNum">     169 </span>            : 
<span class="lineNum">     170 </span><span class="lineCov">     690463 : void SyncedMemory::check_device() {</span>
<span class="lineNum">     171 </span>            : #ifndef CPU_ONLY
<span class="lineNum">     172 </span>            : #ifdef DEBUG
<span class="lineNum">     173 </span>            :   int device;
<span class="lineNum">     174 </span>            :   cudaGetDevice(&amp;device);
<span class="lineNum">     175 </span>            :   CHECK(device == device_);
<span class="lineNum">     176 </span>            :   if (gpu_ptr_ &amp;&amp; own_gpu_data_) {
<span class="lineNum">     177 </span>            :     cudaPointerAttributes attributes;
<span class="lineNum">     178 </span>            :     CUDA_CHECK(cudaPointerGetAttributes(&amp;attributes, gpu_ptr_));
<span class="lineNum">     179 </span>            :     CHECK(attributes.device == device_);
<span class="lineNum">     180 </span>            :   }
<span class="lineNum">     181 </span>            : #endif
<span class="lineNum">     182 </span>            : #endif
<a name="183"><span class="lineNum">     183 </span><span class="lineCov">     690463 : }</span></a>
<span class="lineNum">     184 </span>            : 
<span class="lineNum">     185 </span><span class="lineCov">          2 : }  // namespace caffe</span>
<span class="lineNum">     186 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
